package com.medical.medical.controller.clcontroller;

import com.alibaba.fastjson.JSONArray;
import com.medical.medical.entity.Card;
import com.medical.medical.entity.Drug;
import com.medical.medical.entity.Record;
import com.medical.medical.service.DrugService;
import com.medical.medical.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @authorDesc 古娜拉黑暗之神!
 * @author ChenLi
 * @date 2022-09-14 08:38:49
 * @version 1.0.0
 * @description 诊断报告请求层
 */
@Controller
@Slf4j
@RequestMapping("buydrug")
public class BuyDrugController {
    @Resource
    private RecordService recordService;
    @Resource
    private DrugService drugService;

    //页面大小
    private static final int PAGE_SIZE = 3;

    /**
     * @description 进入user/paydrug
     * @author ChenLi
     * @date 2022-09-14 18:04:20
     * @param
     * @return {@link String}
     */
    @RequestMapping("intopaydrug")
    public String intopaydrug(String drugTotalPrice,Model model){
        log.info("进入user/paydrug页面");
        double totalPrice = 0;
        if (drugTotalPrice != null){
            try {
                totalPrice = Double.valueOf(drugTotalPrice);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                log.info("解析异常");
            }
        }
        model.addAttribute("totalPrice",totalPrice);
        return "user/paydrug";
    }

    /**
     * @description 进入user/rechargeablecard充值卡页面
     * @author ChenLi
     * @date 2022-09-14 18:04:20
     * @param
     * @return {@link String}
     */
    @RequestMapping("intorechargeablecard")
    public String intorechargeablecard(long pCard,Model model,double balance){
        log.info("进入user/rechargeablecard充值卡页面");
        model.addAttribute("pCard",pCard);
        model.addAttribute("balance",balance);
        return "user/rechargeablecard";
    }
    @RequestMapping("tobuydrug")
    public String getBuyDrug(int id,String currentPage, Model model){
        int current = 0;
        int state = 0;
        if (currentPage != null) {
            try {
                current = Integer.valueOf(currentPage);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                log.info("解析异常");
            }
        }
        int allPage = 0;
        int allCount = 0;
        allCount = recordService.getRecordAllCount(id);
        log.info("{}", allCount);

        //查询总条数为0
        if (allCount==0){
            state = -1;
            return "user/buydrug";
        }
        if (allCount % PAGE_SIZE == 0) {
            allPage = allCount / PAGE_SIZE;
        } else {
            allPage = allCount / PAGE_SIZE + 1;
        }
        if (current < 1) {
            current = 1;
        } else if (current > allPage) {
            current = allPage;
        }
        int start = (current - 1) * PAGE_SIZE;

        List<Record> recordList = null;
        state = recordService.getState(id);
        if (state==2){
            recordList = recordService.getReport(id,start,PAGE_SIZE);
            log.info("{}",recordList);
        }
        model.addAttribute("recordList",recordList);
        model.addAttribute("state",state);
        model.addAttribute("current", current);
        model.addAttribute("allPage", allPage);
        log.info("进入user/buydrug页面");
        return "user/buydrug";
    }

    /**
      * @description 查看详情
      * @author ChenLi
      * @date 2022-09-14 13:32:58
      * @param
      * @return {@link String}
      */
    @RequestMapping("view")
    public String view(int recordOrdinal, int recordId, Model model){
        Record record = recordService.viewByRecordOrdinal(recordOrdinal);
        model.addAttribute("record",record);
        List<Drug> drugList = drugService.getDrugByRecordId(recordId);
        //总金额
        double drugTotalPrice = 0;
        for (Drug drug : drugList) {
            drugTotalPrice  += drug.getDrugSinglePrice();
        }
        model.addAttribute("drugList",drugList);
        model.addAttribute("drugTotalPrice",drugTotalPrice);
        log.info("进入user/buydrugview页面");
        return "user/buydrugview";
    }

    /**
      * @description 支付订单
      * @author ChenLi
      * @date 2022-09-26 15:25:29
      * @param
      * @return {@link String}
      */
    @RequestMapping("pay")
    @ResponseBody
    public Object pay(String pId){
        int pId1 = 0;
        if (pId != null){
            try {
                pId1 = Integer.valueOf(pId);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                log.info("解析异常");
            }
        }
        log.info("用户ID:"+pId1);
        Card card = drugService.getCard(pId1);
        if (card==null){
            return JSONArray.toJSONString("empty");
        }
        return JSONArray.toJSONString(card);
    }
    /**
     * @description 修改卡号余额
     * @author ChenLi
     * @date 2022-09-26 15:25:29
     * @param
     * @return {@link String}
     */
    @RequestMapping("paymoney")
    @ResponseBody
    public Object editBalance(String balance){
        double balance1 = 0;
        String info = null;
        if (balance != null){
            try {
                balance1 = Double.valueOf(balance);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                log.info("解析异常");
            }
        }
        boolean flag = drugService.editBalance(balance1);
        log.info("缴费是否成功:{}",flag);
        if (flag){
            info = "success";
        }else {
            info = "false";
        }
        log.info("info:{}",info);
        return JSONArray.toJSONString(info);
    }

    /**
      * @description 充值卡
      * @author ChenLi
      * @date 2022-09-26 18:11:31
      * @param
      * @return {@link String}
      */
    @RequestMapping("rechargeablemoney")
    public String rechargeablemoney(double balance,double rechargeablemoney,Model model){
        String info = null;
        double balance1 = balance + rechargeablemoney;
        boolean flag = drugService.editBalance(balance1);
        log.info("充值是否成功:{}",flag);
        if (flag){
            info = "success";
        }else {
            info = "false";
        }
        log.info("info:{}",info);
        model.addAttribute("info",info);
        return "user/rechargeablecard";
    }
}
